package com.digitaldreamers.framework.scene;

import org.andengine.entity.IEntity;
import org.andengine.entity.scene.Scene;

import com.digitaldreamers.framework.BaseResourceManager;

public abstract class ManagedScene extends Scene {
	protected final boolean mHasLoadingScene;
	protected final float mMinLoadingSceneTime;
	protected float mElapsedLoadingScreneTime;
	protected boolean mIsLoaded;
	protected BaseResourceManager mBaseResourceManager;
	
	public ManagedScene() {
		this(0f);
	}
	
	public ManagedScene(float pMinLoadingSceneTime) {
		mElapsedLoadingScreneTime = 0f;
		mIsLoaded = false;
		mMinLoadingSceneTime = pMinLoadingSceneTime;
		mHasLoadingScene = pMinLoadingSceneTime > 0f;
		mBaseResourceManager = BaseResourceManager.getInstance();
		setOnSceneTouchListenerBindingOnActionDownEnabled(true);
		setTouchAreaBindingOnActionDownEnabled(true);
		setTouchAreaBindingOnActionMoveEnabled(true);
	}
	
	public void onLoadManagedScene() {
		if(!mIsLoaded) {
			onLoadScene();
			mIsLoaded = true;
			setIgnoreUpdate(true);
		}
	}
	
	public void onUnloadManagedScene() {
		if(mIsLoaded) {
			mIsLoaded = false;
			mBaseResourceManager.mEngine.runOnUpdateThread(new Runnable() {
				@Override
				public void run() {
					onUnloadScene();
				}
			});
		}
	}
	
	public void onShowManagedScene() {
		setIgnoreUpdate(false);
		onShowScene();
	}
	
	public void onHideManagedScene() {
		setIgnoreUpdate(true);
		onHideScene();
	}
	
	@Override
	public void attachChild(final IEntity pEntity) throws IllegalStateException {
		pEntity.setScale(mBaseResourceManager.mCameraScaleFactorX, mBaseResourceManager.mCameraScaleFactorY);
		super.attachChild(pEntity);
	}
	
	public abstract Scene onLoadingSceneLoadAndShown();
	public abstract void onLoadingSceneUnloadAndHidden();
	public abstract void onLoadScene();
	public abstract void onShowScene();
	public abstract void onHideScene();
	public abstract void onUnloadScene();
}